# -*- coding: utf-8 -*- 
import settings,wx

def detect():
	settings.screenWidth,settings.screenHeight = wx.ScreenDC().GetSize()

def f2image(filename):
        img = wx.Image(filename, wx.BITMAP_TYPE_ANY)
        w = img.GetWidth()
        h = img.GetHeight()
        sw = settings.screenWidth * 0.75
        sh = settings.screenHeight * 0.75
        
        if w <= sw and h <= sh:
            return img
        
        h2 = h * sw / w
        if h2 > sh:
            w2 = w * sh/ h
            return img.Scale(w2, sh)
        
        return img.Scale( sw, h2)

def min_by_y(d1,d2):
    if d1[1] > d2[1]:return d2
    else:
        return d1
    
def max_by_y(d1,d2):
    if d1[1] < d2[1]:return d2
    else:
        return d1
    
def xdistanceByPixel(a,b):
    x1, y1 = a
    x2, y2 = b
    return abs(x1-x2)

def xdistanceByProp(a,b,proportion):
    x1, y1 = a
    x2, y2 = b
    f = abs(x1 - x2)
    proportion = proportion * 1.0
    return int(f/proportion * 100 + 0.5)/100.0
    
def distanceByPixel(a,b):
    x1, y1 = a
    x2, y2 = b
    f = ((x1-x2)**2 + (y1-y2)**2)**0.5
    return int(f * settings.accuracy + 0.5)/settings.accuracy

def distanceByProp( a, b, proportion ):
	
    x1, y1 = a
    x2, y2 = b
    f = ((x1-x2)**2 + (y1-y2)**2)**0.5
    proportion = proportion * 1.0
    return int(f/proportion * 100 + 0.5)/100.0

    #by inch to cm
    #return int(f/proportion*2.54 * 100 + 0.5)/100.0
    
def sort_dots(dots, d='x'):
	if d=="x":dots.sort()
	else:
		dots.sort(lambda x, y: x[1]-y[1])
	return dots
